********************************************************************************
********************************************************************************
* Figure 5. Composition and service use effects
********************************************************************************
********************************************************************************

********* Panel A, B, and C *********

use "$data/expshares.dta", clear
set more off

* DEFINE VARIABLES
global x 	migch_all_fd
global z	IV2005_all
global w 	totpop13

* CREATE CONTROLS
local msactrl c.feff#i.year c.sh_wap13#i.year c.sh_college13#i.year c.linc13#i.year

* ESTIMATE REGRESSIONS

	// Panel A
local vars sh_ppgds sh_cpgds sh_lawpri sh_water sh_hcserv sh_ed_comp ///
sh_ed_tert sh_sp_dis sh_sp_pen sh_sp_fch sh_sp_hous sh_sp_vul sh_debtserv

tsset area year

local i=1
foreach l of local vars {
rename $x `l'_iv
qui ivreg2 d.`l' (`l'_iv = $z) `msactrl' i.year [aw = $w], robust small
rename `l'_iv $x
estimate store A`i'
local ++i 
}

local plot sh_ppgds_iv sh_cpgds_iv sh_lawpri_iv sh_water_iv  ///
sh_hcserv_iv sh_ed_comp_iv sh_ed_tert_iv sh_sp_dis_iv sh_sp_pen_iv ///
sh_sp_fch_iv sh_sp_hous_iv sh_sp_vul_iv sh_debtserv_iv

coefplot A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13, ///
keep(`plot') xline(0) ciopts(recast(rcap) lcolor(green) lwidth(vthin)) ///
recast(bar) barwidt(0.5) nokey pstyle(p1) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small)) ///
coeflabel(1 sh_ppgds_iv = "‘Pure’ public goods" ///
		  2 sh_cpgds_iv = "‘Congestible’ public goods" ///
		  3 sh_lawpri_iv = "Law courts and prisons" ///
		  4 sh_water_iv = "Water supply" ///
		  5 sh_hcserv_iv = "Health" ///
		  6 sh_ed_comp_iv = "Compulsory education" ///
		  7 sh_ed_tert_iv = "Higher education" ///
		  8 sh_sp_dis_iv = "SP: sickness and disability" ///
		  9 sh_sp_pen_iv = "SP: old age" ///
		  10 sh_sp_fch_iv = "SP: family and children" ///
		  11 sh_sp_hous_iv = "SP: housing" ///
		  12 sh_sp_vul_iv = "SP: vulnerable population" ///
		  13 sh_debtserv_iv = "Debt service", labsize(small))

	// Panel B
local vars_net sh_ppgds_net sh_cpgds_net sh_lawpri_net sh_water_net ///
sh_hcserv_net sh_ed_comp_net sh_ed_tert_net sh_sp_pen_net ///
sh_sp_fch_net sh_sp_hous_net sh_sp_vul_net sh_debtserv_net

local i=1
foreach l of local vars_net {
rename $x `l'_iv
qui ivreg2 d.`l' (`l'_iv = $z) `msactrl' i.year [aw = $w], robust small
rename `l'_iv $x
estimate store B`i'
local ++i 
}

local plot2 sh_ppgds_net_iv sh_cpgds_net_iv sh_lawpri_net_iv ///
sh_water_net_iv sh_hcserv_net_iv sh_ed_comp_net_iv sh_ed_tert_net_iv ///
sh_sp_pen_net_iv sh_sp_fch_net_iv sh_sp_hous_net_iv sh_sp_vul_net_iv ///
sh_debtserv_net_iv

coefplot B1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B11 B12, ///
keep(`plot2') xline(0) ciopts(recast(rcap) lcolor(black) lwidth(vthin)) ///
recast(bar) barwidt(0.5) nokey pstyle(p2) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small)) ///
coeflabel(1 sh_ppgds_net_iv = "‘Pure’ public goods" ///
		  2 sh_cpgds_net_iv = "‘Congestible’ public goods" ///
		  3 sh_lawpri_net_iv = "Law courts and prisons" ///
		  4 sh_water_net_iv = "Water supply" ///
		  5 sh_hcserv_net_iv = "Health" ///
		  6 sh_ed_comp_net_iv = "Compulsory education" ///
		  7 sh_ed_tert_net_iv = "Higher education" ///
		  8 sh_sp_pen_net_iv = "SP: old age" ///
		  9 sh_sp_fch_net_iv = "SP: family and children" ///
		  10 sh_sp_hous_net_iv = "SP: housing" ///
		  11 sh_sp_vul_net_iv = "SP: vulnerable population" ///
		  12 sh_debtserv_net_iv = "Debt service", labsize(small))

	// Panel C
local vars_nat  sh_n_hcserv sh_n_ed_comp sh_n_ed_tert sh_n_sp_dis ///
sh_n_sp_pen sh_n_sp_fch sh_n_sp_hous sh_n_sp_vul
	
matrix A = J(8,3,.)
local i=1
foreach l of local vars_nat {
rename $x `l'_iv
qui ivreg2 d.`l' (`l'_iv = $z) `msactrl' i.year [aw = $w], robust small
matrix A[`i',1] = _b[`l'_iv]
matrix A[`i',2] = _b[`l'_iv] - invttail(e(df_r),0.025)*_se[`l'_iv]
matrix A[`i',3] = _b[`l'_iv] + invttail(e(df_r),0.025)*_se[`l'_iv]
rename `l'_iv $x
local ++i 
}

local vars_nat2 natsh_hcserv natsh_ed_comp natsh_ed_tert ///
natsh_sp_dis natsh_sp_pen natsh_sp_fch natsh_sp_hous natsh_sp_vul
	
matrix B = J(8,3,.)
local i=1
foreach l of local vars_nat2 {
rename $x `l'_iv
qui ivreg2 d.`l' (`l'_iv = $z) `msactrl' i.year [aw = $w], robust small
matrix B[`i',1] = _b[`l'_iv]
matrix B[`i',2] = _b[`l'_iv] - invttail(e(df_r),0.025)*_se[`l'_iv]
matrix B[`i',3] = _b[`l'_iv] + invttail(e(df_r),0.025)*_se[`l'_iv]
rename `l'_iv $x
local ++i 
}

matrix A=A'
matrix B=B'

coefplot (matrix(A[1]), ci((A[2] A[3])) m(C) msize(small) label(Between groups) ciopts(recast(rcap) lwidth(thin))) ///
(matrix(B[1]), axis(2) ci((B[2] B[3])) m(D) msize(small) label(Within group) ciopts(recast(rcap) lwidth(thin))), ///
graphregion(color(white)) xtitle(Between groups effect, size(small)) xtitle(Within group effect, axis(2) size(small)) ///
xlab(, labsize(small)) xlab(, labsize(small) axis(2)) legend(rows(1)) legend(size(small)) ///
coeflabel(1 r1 = "Health" ///
		  2 r2 = "Compulsory education" ///
		  3 r3 = "Higher education" ///
		  4 r4 = "SP: sickness and disability" ///
		  5 r5 = "SP: old age" ///
		  6 r6 = "SP: family and children" ///
		  7 r7 = "SP: housing" ///
		  8 r8 = "SP: vulnerable population", labsize(small))


********* Panel D *********

clear all
use "$data/welfare.dta"
set more off

replace family=1 if family==.
gen lnval = log(real_value)


* CREATE VARIABLE OF INTEREST
gen migch = totmig / totpop13 *100

* DEFINE VARIABLES
global x 	migch
global z	IV2005_pl
global w 	fex12

local depvar welfare health pension cashass lnval
local ctrl age female family real_labinc real_otherinc

matrix A = J(5,3,.)
local i=1
foreach l of local depvar {
rename $x `l'_iv
qui ivreg2 `l' (`l'_iv = $z) `ctrl' i.year i.area [aw = $w], cluster(area) partial(i.area)
matrix A[`i',1] = _b[`l'_iv]
matrix A[`i',2] = _b[`l'_iv] - invttail(e(N)-1-e(df_m),0.025)*_se[`l'_iv]
matrix A[`i',3] = _b[`l'_iv] + invttail(e(N)-1-e(df_m),0.025)*_se[`l'_iv]
rename `l'_iv $x
local ++i 
}

matrix A=A'
matrix A[2,5]=-.03	// truncate CIs for value of transfers for visualization purposes
matrix A[3,5]=.03
 
coefplot (matrix(A[1]), ci((A[2] A[3])) m(C) msize(small) ciopts(recast(rcap) ///
lwidth(thin)) recast(bar) barwidth(0.5) color(*.6)), ///
vertical graphregion(color(white)) ylab(, labsize(small)) xlab(, labsize(small)) nokey ylabel(-0.03(0.01)0.03) yscale(range(-0.03(0.01)0.03)) ///
coeflabel(1 r1 = "Any welfare" ///
		  2 r2 = "Health care" ///
		  3 r3 = "Old age" ///
		  4 r4 = "Cash assistance" ///
		  5 r5 = "Value of transfers" , labsize(small))
